package com.kedll.library.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.view.PointerIconCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.view.menu.MenuItemImpl;
import android.support.v7.view.menu.MenuView;
import android.text.Layout;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kedll.library.R;

/**
 * Created by QiQi on 2017/4/5.
 */

public class BottomNavigationItem extends LinearLayout implements MenuView.ItemView {
    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};

    private ImageView mIcon;
    private TextView mTitle;
    private TextView mMark;

    private MenuItemImpl mItemData;
    private ColorStateList mIconTint;

    private PorterDuff.Mode mTintMode;

    public BottomNavigationItem(Context context) {
        this(context, null);
    }

    public BottomNavigationItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BottomNavigationItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        LayoutInflater.from(context).inflate(R.layout.bottom_navigation_item_layout, this, true);
        mIcon = findViewById(android.R.id.icon);
        mTitle = findViewById(android.R.id.title);
        mMark = findViewById(R.id.mark);
//        setGravity(Gravity.CENTER);
//        setOrientation(LinearLayout.VERTICAL);
//
//        setPadding(0, 10, 0, 10);
//
//        FrameLayout frameLayout = new FrameLayout(context);
//        frameLayout.setDuplicateParentStateEnabled(true);
//        mMark = new TextView(context, attrs, defStyleAttr);
//
//        int padding = (int) (-5 * getResources().getDisplayMetrics().density);
//        FrameLayout.LayoutParams markParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        markParams.gravity = Gravity.TOP | Gravity.RIGHT;
//        markParams.topMargin = padding;
//        markParams.rightMargin = padding;
//
//        frameLayout.addView(mMark, markParams);
//
//        int size = (int) (25 * getResources().getDisplayMetrics().density);
//        FrameLayout.LayoutParams iconParams = new FrameLayout.LayoutParams(size, size);
//        iconParams.gravity = Gravity.CENTER;
//        mIcon = new ImageView(context, attrs, defStyleAttr);
//        mIcon.setDuplicateParentStateEnabled(true);
//
//        frameLayout.addView(mIcon, iconParams);
//
//        addView(frameLayout);
//
//        mTitle = new TextView(context, attrs, defStyleAttr);
//        mTitle.setDuplicateParentStateEnabled(true);
//        mTitle.setGravity(Gravity.CENTER);
////        mTitle.setTextSize(12, TypedValue.COMPLEX_UNIT_DIP);
//        addView(mTitle);
    }


    /**
     * 设置角标数量
     *
     * @param mark
     */
    public void setMark(int mark) {
        if (mark > 0) {
            mMark.setVisibility(View.VISIBLE);
            if (mark > 99) {
                mMark.setText("99+");
            } else {
                mMark.setText(String.valueOf(mark));
            }
        } else {
            mMark.setVisibility(View.GONE);
        }
    }

    /**
     * 设置角标背景色
     *
     * @param resId
     */
    public void setMarkBackground(int resId) {
        if (resId != 0) {
            mMark.setBackgroundResource(resId);
        }
    }

    /**
     * 设置角标字体颜色
     *
     * @param textColor
     */
    public void setMarkTextColor(ColorStateList textColor) {
        if (textColor != null) {
            mMark.setTextColor(textColor);
        }
    }

    @SuppressLint("RestrictedApi")
    @Override
    public void initialize(MenuItemImpl itemData, int menuType) {
        mItemData = itemData;
        if (itemData.showsTextAsAction()) {
            setTitle(itemData.getTitle());
        } else {
            mTitle.setVisibility(GONE);
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mIcon.getLayoutParams();
            params.width = FrameLayout.LayoutParams.MATCH_PARENT;
            params.height = FrameLayout.LayoutParams.MATCH_PARENT;
        }
        setIcon(itemData.getIcon());
        setChecked(itemData.isChecked());
        setCheckable(itemData.isCheckable());
        setEnabled(itemData.isEnabled());
        setId(itemData.getItemId());
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        mIcon.setEnabled(enabled);
        mTitle.setEnabled(enabled);
        if (enabled) {
            ViewCompat.setPointerIcon(this, PointerIconCompat.getSystemIcon(getContext(), PointerIconCompat.TYPE_HAND));
        } else {
            ViewCompat.setPointerIcon(this, null);
        }
    }

    /**
     * Gets the item data that this view is displaying.
     *
     * @return the item data, or null if there is not one
     */
    @Override
    public MenuItemImpl getItemData() {
        return mItemData;
    }

    /**
     * Sets the title of the item view.
     *
     * @param title The title to set.
     */
    @Override
    public void setTitle(CharSequence title) {
        mTitle.setText(title);
    }

    /**
     * Displays the checkbox for the item view.  This does not ensure the item view will be
     * checked, for that use {@link #setChecked}.
     *
     * @param checkable Whether to display the checkbox or to hide it
     */
    @Override
    public void setCheckable(boolean checkable) {
        refreshDrawableState();
    }

    /**
     * Checks the checkbox for the item view.  If the checkbox is hidden, it will NOT be
     * made visible, call {@link #setCheckable(boolean)} for that.
     *
     * @param checked Whether the checkbox should be checked
     */
    @Override
    public void setChecked(boolean checked) {
        refreshDrawableState();
    }

    /**
     * Sets the shortcut for the item.
     *
     * @param showShortcut Whether a shortcut should be shown(if false, the value of
     *                     shortcutKey should be ignored).
     * @param shortcutKey  The shortcut key that should be shown on the ItemView.
     */
    @Override
    public void setShortcut(boolean showShortcut, char shortcutKey) {

    }

    /**
     * Set the icon of this item view.
     *
     * @param icon The icon of this item. null to hide the icon.
     */
    @Override
    public void setIcon(Drawable icon) {
        if (icon != null) {
            Drawable.ConstantState state = icon.getConstantState();
            icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
            DrawableCompat.setTintList(icon, mIconTint);
            if (mTintMode != null) {
                DrawableCompat.setTintMode(icon, mTintMode);
            }
        }
        mIcon.setImageDrawable(icon);
    }


    /**
     * Whether this item view prefers displaying the condensed title rather
     * than the normal title. If a condensed title is not available, the
     * normal title will be used.
     *
     * @return Whether this item view prefers displaying the condensed
     * title.
     */
    @Override
    public boolean prefersCondensedTitle() {
        return false;
    }

    /**
     * Whether this item view shows an icon.
     *
     * @return Whether this item view shows an icon.
     */
    @Override
    public boolean showsIcon() {
        return true;
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (mItemData != null && mItemData.isCheckable() && mItemData.isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }


    public void setIconTintList(ColorStateList tint) {
        mIconTint = tint;
        if (mItemData != null) {
            // Update the icon so that the tint takes effect
            setIcon(mItemData.getIcon());
        }
    }

    public void setTextColor(ColorStateList color) {
        if (color != null) {
            mTitle.setTextColor(color);
        }
    }

    public void setTintMode(PorterDuff.Mode mTintMode) {
        this.mTintMode = mTintMode;
    }

    public void setItemBackground(int background) {
        Drawable backgroundDrawable = background == 0
                ? null : ContextCompat.getDrawable(getContext(), background);
        ViewCompat.setBackground(this, backgroundDrawable);
    }
}
